#include "MainMenuState.h"
//SGD Wrappers
#include "../../SGD_Wrappers/CSGD_Direct3D.h"
#include "../../SGD_Wrappers/CSGD_DirectInput.h"
#include "../../SGD_Wrappers/CSGD_TextureManager.h"
#include "../../SGD_Wrappers/CSGD_XAudio2.h"
#include "../../SGD_Wrappers/SGD_String.h"
#include "../Game.h"
#include "GamePlayState.h"
#include "SettingState.h"
#include "LoadScreen.h"
#include "../SoundSystem/SoundSystem.h"
#include "../Game.h"
#include "GamePlayState.h"
#include "SettingState.h"

#include "../Tiles/TileManager.h"
#include "../DrawText.h"

#include "../Particle System/ParticleManager.h"

CMainMenuState::CMainMenuState()
{
	m_pDT = nullptr;
	m_pTM		= nullptr;
	m_pDI		= nullptr;
	m_pTest		= nullptr;
	m_nCursorPos = 1;
	m_bCredits = false;
	m_bHowToPlay = false;
	SoundSystem::GetInstance()->playSound("DST-AngryRobotIII",true);
	m_nTextImageID = -1;
}

CMainMenuState::~CMainMenuState()
{
	
}

CMainMenuState*	CMainMenuState::GetInstance( void )
{
	//Store an instance in global memory
	static CMainMenuState s_Instance;

	//Return the instance
	return &s_Instance;
}

// IGameState Interface
void	CMainMenuState::Enter	( void )
{
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pDT	= CDrawText::GetInstance();
	m_pDT->ParseFont("text.fnt");
	
	m_nTextImageID = m_pTM->LoadTexture(_T("text_0.png"));

	//std::string XMLLevel = "TileSets/TestLevelXML.xml";
	//CTileManager::GetInstance()->ReadLevel( XMLLevel );
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pTest	= CParticleManager::GetInstance();
	m_pTest->LoadParticle("Particles.xml");
	// Store the SGD Wrapper singletons
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_bCredits = false;
	m_bHowToPlay = false;
	// Store the SGD Wrapper singletons


}

void	CMainMenuState::Exit	( void )
{
	m_pDT = nullptr;
	if(m_pTest != nullptr)
	{
		CParticleManager::DeleteInstance();
		m_pTest = nullptr;
	}
	m_pDI		= nullptr;
	m_pTM		= nullptr;
}

bool	CMainMenuState::Input	( void )
{
	if( m_pDI->KeyDown(DIK_UP) || m_pDI->KeyDown(DIK_DOWN) )
	{
		m_pTest->ActivateEmitter("Blue Explosion");
	}
	if( m_pDI->KeyPressed( DIK_RIGHT ) || m_pDI->KeyPressed( DIK_LEFT ))
	{
		m_pTest->ActivateEmitter("Smoke");
	}

	if(!m_bHowToPlay && !m_bCredits)
	{
		if( m_pDI->KeyPressed( DIK_RETURN ) == true && m_nCursorPos == 1 )
		{
			CGame::GetInstance()->ChangeState( CGamePlayState::GetInstance() );
			return true;
		}
		else if(m_pDI->KeyPressed(DIK_RETURN) && m_nCursorPos == 2)
		{
			CGame::GetInstance()->AddState(CSettingState::GetInstance());
			return true;
		}
		else if(m_pDI->KeyPressed(DIK_RETURN) && m_nCursorPos == 3)
		{
			CGame::GetInstance()->AddState(CLoadScreen::GetInstance());
			return true;
		}
		else if(m_pDI->KeyPressed(DIK_RETURN) && m_nCursorPos == 4)
		{
			m_bHowToPlay = true;
		}
		else if(m_pDI->KeyPressed(DIK_RETURN) && m_nCursorPos == 5)
		{
			m_bCredits = true;
		}
		else if(m_pDI->KeyPressed(DIK_RETURN) && m_nCursorPos == 6)
		{
			CGame::GetInstance()->ChangeState(nullptr);
			CGame::GetInstance()->Shutdown();
			return false;
		}

		if(m_pDI->KeyPressed(DIK_DOWNARROW))
			m_nCursorPos++;
		if(m_pDI->KeyPressed(DIK_UPARROW))
			m_nCursorPos--;

		if(m_nCursorPos < 1)
			m_nCursorPos = 6;
		if(m_nCursorPos > 6)
			m_nCursorPos = 1;
	}
	else if(m_bHowToPlay || m_bCredits)
	{
		if(m_pDI->KeyPressed(DIK_RETURN))
		{
			m_bHowToPlay = m_bCredits = false;
		}
	}
	return true;
}

void	CMainMenuState::Update	( float fElaspedTime )
{
	m_pTest->UpdateActiveEmitter(fElaspedTime);
}

void	CMainMenuState::Render	( void )
{
	if(!m_bHowToPlay && !m_bCredits)
	{
		m_pD3D->Clear( 255, 0, 255 );
		RECT Cursor;
		if(m_nCursorPos == 1)
			{
				Cursor.left = 50;
				Cursor.top = 100;
				Cursor.bottom = 130;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}
		
			if(m_nCursorPos == 2)
			{
				Cursor.left = 50;
				Cursor.top = 140;
				Cursor.bottom = 170;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}
		
			if(m_nCursorPos == 3)
			{
				Cursor.left = 50;
				Cursor.top = 180;
				Cursor.bottom = 210;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}

			if(m_nCursorPos == 4)
			{
				Cursor.left = 50;
				Cursor.top = 220;
				Cursor.bottom = 250;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}

			if(m_nCursorPos == 5)
			{
				Cursor.left = 50;
				Cursor.top = 260;
				Cursor.bottom = 290;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}

			if(m_nCursorPos == 6)
			{
				Cursor.left = 50;
				Cursor.top = 300;
				Cursor.bottom = 330;
				Cursor.right = 90;
				m_pD3D->DrawRect(Cursor,0,255,255);
			}
		
		m_pDT->Print(m_nTextImageID, "Dusk", 125, 0, 3.0f, D3DCOLOR_ARGB(255, 255, 255,255));
 		
		
		m_pDT->Print(m_nTextImageID, "Play",100,100, 1.0f, D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID, "Settings",100,140, 1.0f, D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID, "Choose your profile", 100,180,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID, "How To Play",100,220, 1.0f, D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID, "Credits",100,260, 1.0f, D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID, "Exit",100,300, 1.0f, D3DCOLOR_ARGB(255, 140, 0,0));
	}
	else if(m_bHowToPlay)
	{
		m_pD3D->Clear( 0, 0, 0 );	
		m_pDT->Print(m_nTextImageID,"WASD is movement, mouse cursor is aiming",30,100,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"1-2-3-4 selects your weapons, Space Dashes",30,130,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	}
	else if(m_bCredits)
	{
		m_pD3D->Clear(0,0,0);
		m_pDT->Print(m_nTextImageID,"Peter Nguyen",0,0,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"Skylar Suzuki",0,30,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"Nolan McGlynn",0,60,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"Nick Viselli",0,90,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"Jared Mullens",0,120,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		m_pDT->Print(m_nTextImageID,"Assistant Producer: Shawn Paris",0,150,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	}
	m_pTest->RenderAll();
}

void CMainMenuState::HandleEvent(CEvent* pEvent)
{

}
